﻿#include "chatwidget.h"
#include "ui_chatwidget.h"
#include "apply_protocl.h"
ChatWidget::ChatWidget(ChatSocket* ct,int uid,int fid,QString uname,QString fname,QString mit,QWidget *parent) :
    QWidget(parent),user_id(uid),friend_id(fid),user_name(uname),friendname(fname),commit(mit),chat_socket(ct),
    ui(new Ui::ChatWidget)
{
    ui->setupUi(this);
    if (commit != "")
    {
        //优先显示备注
        ui->windowname->setText(commit);
    }else {
        ui->windowname->setText(friendname);
    }
}

ChatWidget::~ChatWidget()
{
    chat_socket = NULL;
    delete ui;
}

void ChatWidget::on_pushButton_clicked()
{
    //发送按钮处理
    if ("" == ui->input->toHtml())
    {
        QMessageBox::warning(this,"error","请输入消息");
    }else {
        chat_socket->sendMessage(ui->input->toHtml(),user_id,friend_id);
        QDateTime current_date_time =QDateTime::currentDateTime();
        QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
        ui->show->append(QString("[%1]%2").arg(current_date).arg(user_name));
        ui->show->append(ui->input->toHtml());
        ui->input->clear();
    }


}

void ChatWidget::NewMessagePrivate(QJsonObject root)
{
    QString send_time = root[STR_TIME].toString();
    QString data = root[STR_MESSAGE_DATA].toString();
    ui->show->append(QString("[%1]%2").arg(send_time).arg(friendname));
    ui->show->append(data);
    this->setWindowFlags(Qt::WindowStaysOnTopHint);   //设置窗口置于顶层
    this->show();
}
